package com.dang.alex.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dang.alex.model.User;
import com.dang.alex.service.imp.ServiceDAOFactory;
import com.dang.alex.service.imp.UserServiceImp;
import com.dang.alex.util.EmailCode;
import com.dang.alex.util.Md5Code;
import com.dang.alex.util.Sendmail;

/**
 * Servlet implementation class RegistServlet
 */
@WebServlet("/regist.do")
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final String METHOD_REGIST_FORM="0";
	public static final String METHOD_REGIST_STEP1="1";
	public static final String METHOD_REGIST_STEP2="2";
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	String  method=req.getParameter("method");
    	if(method!=null){
    		switch(method){
    		case RegistServlet.METHOD_REGIST_FORM:
    			registTo(req, resp);
    			break;
    		case RegistServlet.METHOD_REGIST_STEP1:
    			registStep1(req, resp);
    			break;
    		case RegistServlet.METHOD_REGIST_STEP2:
    			registStep2(req, resp);
    			
    		}
    	}
    }
    
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}
	private void registStep1(HttpServletRequest req,HttpServletResponse resp){
		User user=new User();  
		try {
			UserServiceImp userService=ServiceDAOFactory.getUserServiceImp();
			user.setEmail(req.getParameter("email"));
			user.setNickname(req.getParameter("nickname"));
			//將密碼加密
			//String pwd=Md5Code.CreateMd5Code(req.getParameter("pasword"));
			user.setPassword(req.getParameter("password"));
			user.setIs_email_verify(false);
			user.setUser_integral(10);
			//生成邮箱验证码
			//String emailCode=EmailCode.CreateEmailCode();
			String emailCode="testtest";
			user.setEmailVerifyCode(emailCode);
			user.setLast_login_time(System.currentTimeMillis());
			user.setLast_login_ip(req.getRemoteAddr());
			
			userService.save(user);
			req.getSession().setAttribute("s_user", user);
			
			Sendmail s=new Sendmail();
			s.sendMail(user.getEmail(), "验证码"+emailCode+"-"+req.getParameter("email"), "等验证");
			req.getRequestDispatcher("/WEB-INF/view/user/verify_form.jsp").forward(req, resp);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
				
		
	}
	
	
	private void registTo(HttpServletRequest req, HttpServletResponse resp){
		
		try {
			req.getRequestDispatcher("/WEB-INF/view/user/register_form.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private void registStep2(HttpServletRequest req, HttpServletResponse resp){
		String code=req.getParameter("code");
		String uid=EmailCode.getUid(code);
		if(uid==null){
			req.setAttribute("verify_err", "邮箱验证码错误，请检查后重输");
			try {
				req.getRequestDispatcher("/WEB-INF/view/user/register_form.jsp").forward(req, resp);
			} catch (ServletException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return ;
		}
		
		String email=uid;
		String vCode=EmailCode.getUUID(code);

		//去数据库比較是否正確
		UserServiceImp userService=ServiceDAOFactory.getUserServiceImp();
		try {
			User u=userService.findByEmail(email);
			if(u!=null&&vCode.trim().equals(u.getEmailVerifyCode())){
				u.setIs_email_verify(true);
				userService.update(u);
				req.getRequestDispatcher("/WEB-INF/view/user/register_ok.jsp").forward(req, resp);
				return ;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
